package com.springdata.dao;

import com.springdata.pojo.CustInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
 * @author xwli
 * @date: 2021/10/5 15:15
 * @desc
 * @since JDK 1.8
 */

/**
 *  符合SpringDataJpa的Dao层接口规范
 *      JpaRepository<操作的实体类类型,实体类中的主键类型>
 *          封装了基本CRUD操作
 *      JpaSpecificationExecutor<操作的实体类类型>
 *          封装了复杂查询(分页)
 */
public interface CustInfoDao extends JpaRepository<CustInfo,Long>, JpaSpecificationExecutor<CustInfo> {

    /**
     * 基于方法命名规则查询
     * @param custNo
     * @return
     */
    CustInfo findByCustNo(Long custNo);

    /**
     * 使用SQL查询
     *
     */
    @Query(value = "select * from cust_info where custname= ?1" , nativeQuery = true)
    CustInfo findJpqlByName(String custName);

    /**
     * 使用JPQL查询
     *  查询的都是实体类和类的属性
     */
    @Query(value = "from CustInfo where custAge= ?1")
    CustInfo findJpqlByAge(String custAge);

    //@Query(value = "from CustInfo where custName= ?1 and custAge = ?2")
    @Query(value = "select * from cust_info where custname= ?1 and custage = ?2" , nativeQuery = true)
    CustInfo findJpqlByNameAndAge(String custName,String custAge);

    /**
     * 更新cust_info
     * @param custNo
     * @param custName
     */
    @Query(value = "update cust_info set custname = ?2 where custno = ?1" , nativeQuery = true)
    @Modifying //标明是写操作
    void updateCustInfo(Long custNo,String custName);
}

